import * as types from "./mutation-types";
import Api from "api/api";

const state = {
  contentList: {},
  currentContent: {},
  searchContentCondition: {
    pageSize: 5,
    title: "",
    status: ""
  }
};

const getters = {
  contentList: state => state.contentList,
  currentContent: state => state.currentContent,
  searchContentCondition: state => state.searchContentCondition
};

const actions = {
  getContentPageS({ commit }, param) {
    return Api.getContentPage(param).then(ret => {
      commit(types.UPDATE_ACTIVITY_LIST, ret.data.data);
      return ret;
    });
  },
  getContentS({ commit }, id) {
    return Api.getContent({ id: id }).then(ret => {
      var tem = ret.data.data;
      tem.startAndEndTime = [];
      tem.startAndEndTime.push(tem.startTime);
      tem.startAndEndTime.push(tem.endTime);
      commit(types.UPDATE_CURRENT_ACTIVITY, tem);
      return ret;
    });
  },
  addOrUpdateContentS({ commit }, payload) {
    return Api.addOrUpdateContent({ body: payload }).then(ret => {
      return ret;
    });
  },

  deleteContentS({ commit }, param) {
    return Api.deleteContent(param).then(ret => {
      return ret;
    });
  }
};

const mutations = {
  [types.INIT_CURRENT_ACTIVITY](state, payload) {
    state.currentContent = {
      title: "",
      startTime: "",
      endTime: "",
      cover: "",
      content: "",
      startAndEndTime: []
    };
  },
  [types.UPDATE_CURRENT_ACTIVITY](state, payload) {
    state.currentContent = payload;
  },
  [types.UPDATE_ACTIVITY_LIST](state, payload) {
    state.contentList = payload;
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
